package com.cow.sram.module.authority;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cow.framework.utils.reflection.BeanToMapUtil;
import com.cow.framework.core.base.BaseComponent;
import com.cow.sram.data.dao.RoleinfoDAO;
import com.cow.sram.data.entity.Resourceinfo;
import com.cow.sram.data.entity.Roleinfo;
import com.cow.sram.data.mapper.RoleinfoMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
/**
 * 角色管理
 * @author Administrator
 *
 */
@Service
public class RoleInfoService extends BaseComponent{
	@Autowired
	RoleinfoDAO roleinfoDAO;
	@Autowired
	RoleinfoMapper roleinfoMapper;
	
	/**根据角色名称模糊查找*/
	public Page<Map> findByRoleName(Map parmMap){
		if (parmMap.get("page") != null && parmMap.get("size") != null) {
			PageHelper.startPage((Integer) parmMap.get("page"), (Integer) parmMap.get("size"));
		}
		Page<Roleinfo> list = roleinfoMapper.queryRoleinfo(parmMap);
		com.github.pagehelper.Page<Map> resultList = new com.github.pagehelper.Page<Map>();
		BeanUtils.copyProperties(list, resultList);
		for (Roleinfo resInfo : list) {
			resultList.add(convertToMap(resInfo));
		}
		return resultList;
	}
	
	//转译
		private Map convertToMap(Roleinfo roleinfo) {
			Map dictMap = new HashMap();
			dictMap.put("roletype", "roleType");
			Map map = BeanToMapUtil.convertBean(roleinfo);
			dictUtil.valueToName(map, dictMap);
			return map;
		}
		
	
	/**根据ID查询角色*/
	public Roleinfo getRoleinfoById(Integer id){
		return roleinfoDAO.findOne(id);
	}
	
	/**新增角色*/
	public Roleinfo saveRoleinfo(Roleinfo roleinfo){
		return roleinfoDAO.save(roleinfo);
	}
	
	
	
	/**删除角色*/
	public void delRoleinfo(Integer id){
		Roleinfo roleinfo =roleinfoDAO.findOne(id);
		roleinfoDAO.delete(roleinfo);
//		roleinfoDAO.delete(id);
	}

}
